package matlap.client.view;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

import java.util.Iterator;
import java.util.List;
import com.google.gwt.user.client.ui.Button;

import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.MenuItemSeparator;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ListBox;

import matlap.client.services.registration.RegService;
import matlap.client.services.registration.RegServiceAsync;
import matlap.client.services.school.SchoolService;
import matlap.client.services.school.SchoolServiceAsync;
import matlap.client.services.user.UserService;
import matlap.client.services.user.UserServiceAsync;
import matlap.client.view.admin.AdminMenuComposite;
import matlap.client.view.distributor.DistributorMenuComposite;
import matlap.client.view.guest.ArticlesComposite;
import matlap.client.view.guest.ContactComposite;
import matlap.client.view.guest.HomePageComposite;
import matlap.client.view.guest.NewsComposite;
import matlap.client.view.guest.RegInfoComposite;
import matlap.client.view.guest.SupportersComposite;
import matlap.client.view.guest.TeamComposite;
import matlap.client.view.student.StudentMenuComposite;
import matlap.client.view.teacher.TeacherMenuComposite;
import matlap.shared.School;
import matlap.shared.Student;
import matlap.shared.User;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */

public class MatlapGWT implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	public MatlapGWT() {
	}

	UserServiceAsync a1 = GWT.create(UserService.class);
	SchoolServiceAsync s1 = GWT.create(SchoolService.class);
	RegServiceAsync c1 = GWT.create(RegService.class);
	public HorizontalPanel bigPanel;
	public VerticalPanel extraMenu;
	public VerticalPanel loginPanel;
	//	public VerticalPanel logoutPanel;
	public HorizontalPanel logoutPanel;

	public VerticalPanel registrationPanel;
	private MenuItem mntmRolunk;
	private MenuItem mntmHirek;
	private MenuItem mntmCikkek;
	private MenuItem mntmElerhetosegek;
	private MenuItem mntmTudnivalok;
	private MenuItem mntmKezdolap;
	private boolean clicked;
	private AdminMenuComposite am;
	private StudentMenuComposite sm;
	private TeacherMenuComposite tm;
	private DistributorMenuComposite dm;
	private Label nameLabel;

	private TextBox firstNameBoxReg;
	private TextBox lastNameBoxReg;
	private TextBox addressBoxReg;
	private TextBox telefonBoxReg;
	private TextBox emailBoxReg;
	private PasswordTextBox passwordTextBoxReg;
	private ListBox roleBoxReg;
	private CheckBox checkBoxReg;
	private Label schoolLabelReg;
	private ListBox townBoxReg;
	private Label schoolNameLabelReg;
	private ListBox schoolComboBoxReg;
	private Label classLabelReg;
	private ListBox classBoxReg;

	public boolean isClicked() {
		return clicked;
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		RootPanel rootPanel = RootPanel.get();
		rootPanel.setStyleName("rootPanel");
		rootPanel.setSize("1200", "800");

		initRegistrationPanel();
		Image widget = new Image("/matlap_logo.jpg");
		RootPanel.get().add(widget, 170, 35);
		widget.setSize("105px", "136px");

		bigPanel = new HorizontalPanel();
		rootPanel.add(bigPanel, 222, 293);
		bigPanel.setSize("433px", "227px");
		HomePageComposite k = new HomePageComposite();
		bigPanel.add(k);
		k.setSize("541px", "500");

		MenuBar menuBar = new MenuBar(false);
		menuBar.setStyleName("MenuBar");
		rootPanel.add(menuBar, -32, 47);
		menuBar.setSize("759px", "27px");

		mntmKezdolap = new MenuItem("Kezd\u0151lap", false, new Command() {
			public void execute() {
				HomePageComposite b = new HomePageComposite();
				b.setSize("500", "500");
				bigPanel.clear();
				bigPanel.add(b);

				mntmKezdolap.setStyleName("SelectedMenuItem");
				mntmRolunk.setStyleName("MenuItem");
				mntmHirek.setStyleName("MenuItem");
				mntmCikkek.setStyleName("MenuItem");
				mntmElerhetosegek.setStyleName("MenuItem");
				mntmTudnivalok.setStyleName("MenuItem");
				clearRegInfo();
				addStudentsInfo(false);
				if (am != null)
					am.setUnSelectedMenuItems();
				if (sm != null)
					sm.setUnSelectedMenuItems();
				if (tm != null)
					tm.setUnSelectedMenuItems();
				if (dm != null)
					dm.setUnSelectedMenuItems();
			}

		});

		mntmKezdolap.setStyleName("SelectedMenuItem");
		menuBar.addItem(mntmKezdolap);
		mntmKezdolap.setSize("", "");

		MenuItemSeparator separator_3 = new MenuItemSeparator();
		separator_3.setStyleName("MenuItemSeparator");
		menuBar.addSeparator(separator_3);
		separator_3.setSize("", "");

		mntmRolunk = new MenuItem("R\u00F3lunk", false, new Command() {
			public void execute() {
				TeamComposite b = new TeamComposite();
				bigPanel.clear();
				bigPanel.add(b);
				mntmKezdolap.setStyleName("MenuItem");
				mntmRolunk.setStyleName("SelectedMenuItem");
				mntmHirek.setStyleName("MenuItem");
				mntmCikkek.setStyleName("MenuItem");
				mntmElerhetosegek.setStyleName("MenuItem");
				mntmTudnivalok.setStyleName("MenuItem");
				clearRegInfo();
				addStudentsInfo(false);
				if (am != null)
					am.setUnSelectedMenuItems();
				if (sm != null)
					sm.setUnSelectedMenuItems();
				if (tm != null)
					tm.setUnSelectedMenuItems();
				if (dm != null)
					dm.setUnSelectedMenuItems();
			}
		});

		mntmRolunk.setStyleName("MenuItem");
		menuBar.addItem(mntmRolunk);
		mntmRolunk.setSize("", "");

		MenuItemSeparator separator_2 = new MenuItemSeparator();
		separator_2.setStyleName("MenuItemSeparator");
		menuBar.addSeparator(separator_2);
		separator_2.setSize("", "");

		mntmHirek = new MenuItem("H\u00EDrek", false, new Command() {
			public void execute() {
				NewsComposite h = new NewsComposite();
				h.setSize("500", "500");
				bigPanel.clear();
				bigPanel.add(h);
				mntmKezdolap.setStyleName("MenuItem");
				mntmRolunk.setStyleName("MenuItem");
				mntmHirek.setStyleName("SelectedMenuItem");
				mntmCikkek.setStyleName("MenuItem");
				mntmElerhetosegek.setStyleName("MenuItem");
				mntmTudnivalok.setStyleName("MenuItem");
				clearRegInfo();
				addStudentsInfo(false);
				clicked = true;
				if (am != null)
					am.setUnSelectedMenuItems();
				if (sm != null)
					sm.setUnSelectedMenuItems();
				if (tm != null)
					tm.setUnSelectedMenuItems();
				if (dm != null)
					dm.setUnSelectedMenuItems();
			}
		});
		mntmHirek.setStyleName("MenuItem");
		menuBar.addItem(mntmHirek);
		mntmHirek.setSize("", "");

		MenuItemSeparator separator_1 = new MenuItemSeparator();
		separator_1.setStyleName("MenuItemSeparator");
		menuBar.addSeparator(separator_1);
		separator_1.setSize("", "");

		mntmCikkek = new MenuItem("Cikkek", false, new Command() {
			public void execute() {
				bigPanel.clear();
				mntmKezdolap.setStyleName("MenuItem");
				mntmRolunk.setStyleName("MenuItem");
				mntmHirek.setStyleName("MenuItem");
				mntmCikkek.setStyleName("SelectedMenuItem");
				mntmElerhetosegek.setStyleName("MenuItem");
				mntmTudnivalok.setStyleName("MenuItem");
				clearRegInfo();
				addStudentsInfo(false);
				if (am != null)
					am.setUnSelectedMenuItems();
				if (sm != null)
					sm.setUnSelectedMenuItems();
				if (tm != null)
					tm.setUnSelectedMenuItems();
				if (dm != null)
					dm.setUnSelectedMenuItems();
				new ArticlesComposite(bigPanel);
			}
		});
		mntmCikkek.setStyleName("MenuItem");
		menuBar.addItem(mntmCikkek);
		mntmCikkek.setSize("", "");

		MenuItemSeparator separator = new MenuItemSeparator();
		separator.setStyleName("MenuItemSeparator");
		menuBar.addSeparator(separator);
		separator.setSize("", "");

		mntmElerhetosegek = new MenuItem("El\u00E9rhet\u0151s\u00E9gek", false,
				new Command() {
			public void execute() {
				ContactComposite b = new ContactComposite();
				b.setSize("200", "300");
				bigPanel.clear();
				bigPanel.add(b);
				mntmKezdolap.setStyleName("MenuItem");
				mntmRolunk.setStyleName("MenuItem");
				mntmHirek.setStyleName("MenuItem");
				mntmCikkek.setStyleName("MenuItem");
				mntmElerhetosegek.setStyleName("SelectedMenuItem");
				mntmTudnivalok.setStyleName("MenuItem");
				clearRegInfo();
				addStudentsInfo(false);
				if (am != null)
					am.setUnSelectedMenuItems();
				if (sm != null)
					sm.setUnSelectedMenuItems();
				if (tm != null)
					tm.setUnSelectedMenuItems();
				if (dm != null)
					dm.setUnSelectedMenuItems();
			}
		});
		mntmElerhetosegek.setStyleName("MenuItem");
		menuBar.addItem(mntmElerhetosegek);
		mntmElerhetosegek.setSize("100", "20");

		MenuItemSeparator separator4 = new MenuItemSeparator();
		separator4.setStyleName("MenuItemSeparator");
		separator.setStyleName("MenuItemSeparator");
		menuBar.addSeparator(separator4);
		separator4.setSize("", "");
		separator.setSize("", "");

		mntmTudnivalok = new MenuItem("Tudnival\u00F3k", false, new Command() {
			public void execute() {
				bigPanel.clear();
				RegInfoComposite t = new RegInfoComposite();
				bigPanel.add(t);
				mntmKezdolap.setStyleName("MenuItem");
				mntmRolunk.setStyleName("MenuItem");
				mntmHirek.setStyleName("MenuItem");
				mntmCikkek.setStyleName("MenuItem");
				mntmElerhetosegek.setStyleName("MenuItem");
				mntmTudnivalok.setStyleName("SelectedMenuItem");
				clearRegInfo();
				addStudentsInfo(false);
				if (am != null)
					am.setUnSelectedMenuItems();
				if (sm != null)
					sm.setUnSelectedMenuItems();
				if (tm != null)
					tm.setUnSelectedMenuItems();
				if (dm != null)
					dm.setUnSelectedMenuItems();
			}
		});
		mntmTudnivalok.setStyleName("MenuItem");
		menuBar.addItem(mntmTudnivalok);
		mntmElerhetosegek.setSize("100", "20");

		Label lblNewLabel = new Label("MatLap");
		lblNewLabel.setStyleName("CimLabel");
		rootPanel.add(lblNewLabel, 694, 35);
		lblNewLabel.setSize("201px", "69px");

		Label lblNewLabel_1 = new Label(
		"Ifj\u00FAs\u00E1gi matematikai lapok\r\n");
		lblNewLabel_1.setStyleName("CimLabel");
		rootPanel.add(lblNewLabel_1, 304, 110);
		lblNewLabel_1.setSize("593px", "61px");
		;

		loginPanel = new VerticalPanel();
		RootPanel.get().add(loginPanel, 954, 36);
		loginPanel.setSize("350px", "196px");
		loginPanelInit();

		VerticalPanel tamogatoPanel = new VerticalPanel();
		rootPanel.add(tamogatoPanel, 1046, 238);
		tamogatoPanel.setSize("171px", "492px");
		SupportersComposite t = new SupportersComposite();
		tamogatoPanel.add(t);
		t.setWidth("171px");

		extraMenu = new VerticalPanel();
		rootPanel.add(extraMenu, 39, 273);
		extraMenu.setSize("143px", "231px");

		//		logoutPanel = new VerticalPanel();
		logoutPanel = new HorizontalPanel();
		rootPanel.add(logoutPanel, 62, 202);
		logoutPanel.setSize("118px", "66px");
		logoutPanelInit();
		logoutPanel.setVisible(false);

	}

	@SuppressWarnings("deprecation")
	public void loginPanelInit() {
		Label lblLoginToYour = new Label("Bejelentkez\u00E9s");
		lblLoginToYour.setStyleName("LoginLabel");
		loginPanel.add(lblLoginToYour);
		loginPanel.setCellHorizontalAlignment(lblLoginToYour,
				HasHorizontalAlignment.ALIGN_CENTER);
		lblLoginToYour.setWidth("91px");

		FlexTable flexTable = new FlexTable();
		loginPanel.add(flexTable);
		flexTable.setWidth("350px");

		Label lblUsername = new Label("N\u00E9v:");
		lblUsername.setStyleName("LoginLabel");
		flexTable.setWidget(0, 0, lblUsername);

		final TextBox textBoxUsername = new TextBox();
		flexTable.setWidget(0, 1, textBoxUsername);
		textBoxUsername.setWidth("150px");

		Label lblPassword = new Label("Jelsz\u00F3:");
		lblPassword.setStyleName("LoginLabel");
		flexTable.setWidget(1, 0, lblPassword);

		final PasswordTextBox textBoxPassword = new PasswordTextBox();
		flexTable.setWidget(1, 1, textBoxPassword);
		textBoxPassword.setWidth("150px");

		Button btnSignIn = new Button("Bel\u00E9p");
		btnSignIn.setStyleName("ButtonStyle");
		flexTable.setWidget(3, 1, btnSignIn);
		btnSignIn.setWidth("62px");
		btnSignIn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				final String s1 = textBoxUsername.getText();
				mntmKezdolap.setStyleName("SelectedMenuItem");
				mntmRolunk.setStyleName("MenuItem");
				mntmHirek.setStyleName("MenuItem");
				mntmCikkek.setStyleName("MenuItem");
				mntmElerhetosegek.setStyleName("MenuItem");
				mntmTudnivalok.setStyleName("MenuItem");
				String s2 = textBoxPassword.getText();
				if (s1.length() == 0 || s2.length() == 0) {
					Window.alert("Nem adott meg nevet vagy jelsz\u00F3t!");
				}

				else {
					a1.loginUser(s1, s2, new AsyncCallback<User>() {

						public void onSuccess(User user) {
							if (user.isLoggedIn()) {
								loginPanel.setVisible(false);
								textBoxUsername.setText("");
								textBoxPassword.setText("");
								bigPanel.clear();
								HomePageComposite hpc = new HomePageComposite();
								bigPanel.add(hpc);
								nameLabel.setText(s1);
								logoutPanel.setVisible(true);
								if (user.getRole() == 0) {
									am = new AdminMenuComposite(bigPanel, s1,
											getThis());
									extraMenu.add(am);
								}

								else if (user.getRole() == 1) {
									sm = new StudentMenuComposite(bigPanel, s1,
											getThis());
									extraMenu.add(sm);
								} else if (user.getRole() == 2) {
									System.out.println(user.getAccept());
									if (user.getAccept() == 1) {
										tm = new TeacherMenuComposite(bigPanel,
												s1, getThis());
										extraMenu.add(tm);
									} else {
										loginPanel.setVisible(true);
										textBoxUsername.setText("");
										textBoxPassword.setText("");
										nameLabel.setText("");
										logoutPanel.setVisible(false);
										Window.alert("Az admin még nem hagyta jóvá a belépésed.Próbálkozz később.");
									}
								} else if (user.getRole() == 3) {
									dm = new DistributorMenuComposite(bigPanel,
											s1, getThis());
									extraMenu.add(dm);
								}

							} else if (user.getMessage()
									.equals("wrongPassword")) {
								Window.alert("Sikertelen bel\u00E9p\u00E9s!Rossz jelsz\u00F3!");

							} else if (user.getMessage().equals("wrongUser")) {
								Window.alert("Sikertelen bel\u00E9p\u00E9s!Nem regisztrl\u00E1t felhaszn\u00E1l\u00F3n\u00E9v!");
							}

						}

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Sikertelen bel\u00E9p\u00E9s");
						}
					});

				}

			}

		});

		Hyperlink hprlnkNewHyperlink = new Hyperlink("Regisztr\u00E1lj most!",
				false, "newHistoryToken");
		hprlnkNewHyperlink.setStyleName("Hyperlink");
		hprlnkNewHyperlink.setDirectionEstimator(false);
		hprlnkNewHyperlink.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				bigPanel.clear();
				bigPanel.add(registrationPanel);

			}

		});

		loginPanel.add(hprlnkNewHyperlink);
		loginPanel.setCellHorizontalAlignment(hprlnkNewHyperlink,
				HasHorizontalAlignment.ALIGN_CENTER);
		hprlnkNewHyperlink.setWidth("110px");

		Hyperlink hprlnkNewHyperlink_1 = new Hyperlink(
				"Elfelejtette jelszav\u00E1t? ", false, "newHistoryToken");
		hprlnkNewHyperlink_1.setStyleName("Hyperlink");
		loginPanel.add(hprlnkNewHyperlink_1);
		hprlnkNewHyperlink_1.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				Window.alert("K\u00E9rem a matlap2007@yahoo.com e-mail c\u00EDmen \u00E9rtes\u00EDtsen a sz\u00FCks\u00E9ges jelsz\u00F3cser\u00E9r\u0151l!");

			}

		});

		loginPanel.setCellHorizontalAlignment(hprlnkNewHyperlink_1,
				HasHorizontalAlignment.ALIGN_CENTER);
	}

	public void logoutPanelInit() {

		Button btLogout = new Button("Kil\u00E9p\u00E9s");
		btLogout.setStyleName("ButtonStyle");
		logoutPanel.add(btLogout);
		btLogout.setSize("116px", "33px");
		btLogout.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				mntmKezdolap.setStyleName("SelectedMenuItem");
				mntmRolunk.setStyleName("MenuItem");
				mntmHirek.setStyleName("MenuItem");
				mntmCikkek.setStyleName("MenuItem");
				mntmElerhetosegek.setStyleName("MenuItem");
				mntmTudnivalok.setStyleName("MenuItem");
				logoutPanel.setVisible(false);
				loginPanel.setVisible(true);
				extraMenu.clear();
				bigPanel.clear();
				HomePageComposite hpc = new HomePageComposite();
				bigPanel.add(hpc);
			}

		});

		nameLabel = new Label("New label");
		nameLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		nameLabel.setStyleName("BigBold");
		logoutPanel.add(nameLabel);
		nameLabel.setSize("105px", "25px");
	}

	public void initRegistrationPanel() {
		registrationPanel = new VerticalPanel();

		FlexTable flexTable = new FlexTable();
		registrationPanel.add(flexTable);
		flexTable.setSize("329px", "462px");

		Label lblNewLabel_8 = new Label("Regisztr\u00E1ci\u00F3");
		lblNewLabel_8.setStyleName("BigBold");
		flexTable.setWidget(0, 1, lblNewLabel_8);

		Label lblNewLabel_1 = new Label("Vezet\u00E9kn\u00E9v*");
		lblNewLabel_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		lblNewLabel_1.setStyleName("Bold");

		flexTable.setWidget(1, 0, lblNewLabel_1);
		lblNewLabel_1.setSize("350", "50");

		firstNameBoxReg = new TextBox();
		flexTable.setWidget(1, 1, firstNameBoxReg);

		Label lblNewLabel_2 = new Label("Keresztn\u00E9v*");
		flexTable.setWidget(2, 0, lblNewLabel_2);
		lblNewLabel_2.setStyleName("Bold");

		lastNameBoxReg = new TextBox();
		flexTable.setWidget(2, 1, lastNameBoxReg);

		Label lblNewLabel_4 = new Label("Lakc\u00EDm*");
		flexTable.setWidget(3, 0, lblNewLabel_4);
		lblNewLabel_4.setStyleName("Bold");

		addressBoxReg = new TextBox();
		flexTable.setWidget(3, 1, addressBoxReg);
		addressBoxReg.setWidth("220px");

		Label lblNewLabel_6 = new Label("E-mail c\u00EDm*");
		flexTable.setWidget(4, 0, lblNewLabel_6);
		lblNewLabel_6.setStyleName("Bold");

		emailBoxReg = new TextBox();
		flexTable.setWidget(4, 1, emailBoxReg);

		Label lblNewLabel_7 = new Label("Telefonsz\u00E1m");
		flexTable.setWidget(5, 0, lblNewLabel_7);
		lblNewLabel_7.setStyleName("Bold");

		telefonBoxReg = new TextBox();
		flexTable.setWidget(5, 1, telefonBoxReg);

		Label lblNewLabel_12 = new Label("Jelsz\u00F3*");
		flexTable.setWidget(6, 0, lblNewLabel_12);

		passwordTextBoxReg = new PasswordTextBox();
		flexTable.setWidget(6, 1, passwordTextBoxReg);

		Label lblNewLabel_3 = new Label("Szerepk\u00F6r*");
		flexTable.setWidget(7, 0, lblNewLabel_3);
		lblNewLabel_3.setStyleName("Bold");

		roleBoxReg = new ListBox();
		roleBoxReg.addItem("");
		roleBoxReg.addItem("Feladatmegold\u00F3");
		roleBoxReg.addItem("Jav\u00EDt\u00F3");
		roleBoxReg.addItem("Megrendel\u0151 ");
		flexTable.setWidget(7, 1, roleBoxReg);
		roleBoxReg.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (roleBoxReg.getSelectedIndex() == 1) {
					addStudentsInfo(true);
				} else {
					addStudentsInfo(false);
				}

			}

		});

		checkBoxReg = new CheckBox("K\u00FCld\u00F6tt m\u00E1r be feladatokat");
		flexTable.setWidget(8, 1, checkBoxReg);
		checkBoxReg.setVisible(false);

		schoolLabelReg = new Label("Iskola v\u00E1rosa*");
		schoolLabelReg.setVisible(false);
		flexTable.setWidget(9, 0, schoolLabelReg);

		townBoxReg = new ListBox();
		townBoxReg.addItem("");
		townBoxReg.setVisible(false);
		flexTable.setWidget(9, 1, townBoxReg);
		s1.selectLocality(new AsyncCallback<List<String>>() {

			public void onFailure(Throwable caught) {
			}

			public void onSuccess(List<String> result) {
				Iterator<String> iterator = result.iterator();
				while (iterator.hasNext()) {
					townBoxReg.addItem(iterator.next());
				}
			}

		});
		townBoxReg.addItem("m\u00E1s");
		townBoxReg.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				String loc = townBoxReg.getItemText(townBoxReg
						.getSelectedIndex());
				updateSchoolBox(loc);
			}

		});

		schoolNameLabelReg = new Label("Iskola neve*");
		flexTable.setWidget(10, 0, schoolNameLabelReg);
		schoolNameLabelReg.setStyleName("Bold");
		schoolNameLabelReg.setVisible(false);

		schoolComboBoxReg = new ListBox();
		schoolComboBoxReg.addItem("");
		s1.selectSchoolsName(new AsyncCallback<List<School>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Database problem");

			}

			public void onSuccess(List<School> result) {
				Iterator<School> iterator = result.iterator();
				while (iterator.hasNext()) {
					schoolComboBoxReg.addItem(iterator.next().getName());
				}
			}

		});
		schoolComboBoxReg.addItem("m\u00E1s");
		flexTable.setWidget(10, 1, schoolComboBoxReg);
		schoolComboBoxReg.setVisible(false);
		schoolComboBoxReg.setWidth("215px");

		classLabelReg = new Label("Oszt\u00E1ly*");
		classLabelReg.setVisible(false);
		flexTable.setWidget(11, 0, classLabelReg);

		classBoxReg = new ListBox();
		classBoxReg.setVisible(false);
		flexTable.setWidget(11, 1, classBoxReg);
		classBoxReg.addItem("");
		classBoxReg.addItem("1");
		classBoxReg.addItem("2");
		classBoxReg.addItem("3");
		classBoxReg.addItem("4");
		classBoxReg.addItem("5");
		classBoxReg.addItem("6");
		classBoxReg.addItem("7");
		classBoxReg.addItem("8");
		classBoxReg.addItem("9");
		classBoxReg.addItem("10");
		classBoxReg.addItem("11");
		classBoxReg.addItem("12");

		Label labelReg = new Label("* : k\u00F6telez\u0151 kit\u00F6lteni");
		flexTable.setWidget(12, 1, labelReg);

		Button btnNewButton = new Button("Regisztr\u00E1l");
		flexTable.setWidget(13, 1, btnNewButton);
		btnNewButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String firstName = lastNameBoxReg.getText();
				String lastName = firstNameBoxReg.getText();
				String address = addressBoxReg.getText();
				String telephone = telefonBoxReg.getText();
				String email = emailBoxReg.getText();
				final String password = passwordTextBoxReg.getText();

				final int n = roleBoxReg.getSelectedIndex();
				boolean check = checkBoxReg.getValue();
				int class_nr = classBoxReg.getSelectedIndex() + 1;
				String town = townBoxReg.getItemText(townBoxReg
						.getSelectedIndex());
				String school = schoolComboBoxReg.getItemText(schoolComboBoxReg
						.getSelectedIndex());

				if (firstName.length() == 0) {
					Window.alert("Nem adott meg vezet\u00E9knevet!");
				} else if (lastName.length() == 0) {
					Window.alert("Nem adott meg keresztnevet!");
				} else if (address.length() == 0) {
					Window.alert("Nem adott meg c\u00EDmet!");
				} else if (email.length() == 0) {
					Window.alert("Nem adott meg e-mail c\u00EDmet! ");

				} else if (password.length() == 0) {
					Window.alert("Nem adott meg jelsz\u00F3t!");
				} else if (password.length() < 6) {
					Window.alert("R\u00F6vid jelsz\u00F3!");
				} else if (n == 0) {
					Window.alert("Nem adta meg a szerepk\u00F6rt");
				} else if (n == 1) {
					if (town.length() == 0) {
						Window.alert("Nem adta meg az iskola v\u00E1ros\u00E1t!");
					}
					if (school.length() == 0) {
						Window.alert("Nem adott meg iskol\u00E1t!");
					} else if (class_nr == 0) {
						Window.alert("Nem adott meg oszt\u00E1lyt!");
					} else if (check) {
						Student student = new Student(firstName, lastName, "",
								password, address, email, telephone, "", "", 0);
						c1.insertIntoRegistered(student,
								new AsyncCallback<String>() {

							public void onFailure(Throwable caught) {
								Window.alert("Database problem");

							}

							public void onSuccess(final String result) {
								if (result.equals("")) {
									Window.alert("Sikertelen regisztr\u00E1ci\u00F3!");
								} else {
									Window.alert("Sikeres regisztr\u00E1ci\u00F3!\nAz \u00F6n felhaszn\u00E1l\u00F3neve: "
											+ result
											+ "!\n\u00DCdv az oldalon!");

									bigPanel.clear();
									HomePageComposite hpc = new HomePageComposite();
									bigPanel.add(hpc);
									loginPanel.setVisible(false);
									nameLabel.setText(result);
									logoutPanel.setVisible(true);
									sm = new StudentMenuComposite(
											bigPanel, result, getThis());
									extraMenu.add(sm);
									clearRegInfo();
									addStudentsInfo(false);

								}

							}

						});

					} else {

						Student student = new Student(firstName, lastName, "",
								password, address, email, telephone, school,
								"", class_nr);
						c1.insertPeople(n, student,
								new AsyncCallback<String>() {

							public void onFailure(Throwable caught) {
								Window.alert("Database problem");

							}

							public void onSuccess(final String result) {
								if (result.equals("")) {
									Window.alert("Sikertelen regisztr\u00E1ci\u00F3!");
								} else {
									Window.alert("Sikeres regisztr\u00E1ci\u00F3!\nAz \u00F6n felhaszn\u00E1l\u00F3neve: "
											+ result
											+ "!\n\u00DCdv az oldalon!");

									bigPanel.clear();
									HomePageComposite hpc = new HomePageComposite();
									bigPanel.add(hpc);
									loginPanel.setVisible(false);
									nameLabel.setText(result);
									logoutPanel.setVisible(true);
									sm = new StudentMenuComposite(
											bigPanel, result, getThis());
									extraMenu.add(sm);
									clearRegInfo();
									addStudentsInfo(false);

								}
							}

						});
					}
				} else {
					Student student = new Student(firstName, lastName, "",
							password, address, email, telephone, "", "", 0);
					c1.insertPeople(n, student, new AsyncCallback<String>() {

						public void onFailure(Throwable caught) {
							Window.alert("Database problem");

						}

						public void onSuccess(final String result) {
							if (result.equals("")) {
								Window.alert("Sikertelen regisztr\u00E1ci\u00F3!");
							} else {


								bigPanel.clear();
								HomePageComposite hpc = new HomePageComposite();
								bigPanel.add(hpc);
								if (n == 3) {
									Window.alert("Sikeres regisztr\u00E1ci\u00F3!\nAz \u00F6n felhaszn\u00E1l\u00F3neve: "
											+ result + "!\n\u00DCdv az oldalon!");
									dm = new DistributorMenuComposite(bigPanel,
											result, getThis());
									extraMenu.add(dm);

									loginPanel.setVisible(false);
									nameLabel.setText(result);
									logoutPanel.setVisible(true);
									clearRegInfo();
								}
								else
									clearRegInfo();
									Window.alert("Sikeres regisztr\u00E1ci\u00F3!\nAz \u00F6n felhaszn\u00E1l\u00F3neve: "
											+ result + "!\n Az adminisztr\u00E1tor j\u00F3v\u00E1hagy\u00E1sa ut\u00E1n bel\u00E9phet!");

							}

						}

					});
				}

			}

		});

	}

	public void clearRegInfo() {
		firstNameBoxReg.setText("");
		lastNameBoxReg.setText("");
		addressBoxReg.setText("");
		emailBoxReg.setText("");
		telefonBoxReg.setText("");
		passwordTextBoxReg.setText("");
		roleBoxReg.setSelectedIndex(0);
		checkBoxReg.setValue(false);
		townBoxReg.setSelectedIndex(0);
		schoolComboBoxReg.setSelectedIndex(0);
		classBoxReg.setSelectedIndex(0);

	}

	public void addStudentsInfo(boolean b) {
		checkBoxReg.setVisible(b);
		checkBoxReg.setEnabled(b);
		schoolLabelReg.setVisible(b);
		schoolNameLabelReg.setVisible(b);
		townBoxReg.setVisible(b);
		townBoxReg.setEnabled(b);
		schoolComboBoxReg.setVisible(b);
		schoolComboBoxReg.setEnabled(b);
		classLabelReg.setVisible(b);
		classBoxReg.setVisible(b);
		classBoxReg.setEnabled(b);
	}

	public void updateSchoolBox(String loc) {
		schoolComboBoxReg.clear();
		schoolComboBoxReg.addItem("");
		if (loc.equals("")) {
			s1.selectSchoolsName(new AsyncCallback<List<School>>() {

				public void onFailure(Throwable caught) {
					Window.alert("Database problem");

				}

				public void onSuccess(List<School> result) {
					Iterator<School> iterator = result.iterator();
					while (iterator.hasNext()) {
						schoolComboBoxReg.addItem(iterator.next().getName());
					}
				}

			});
		} else if (loc.equals("m\u00E1s")) {

		} else {
			s1.selectSchoolsNameByLocality(loc,
					new AsyncCallback<List<String>>() {

				public void onFailure(Throwable caught) {
					Window.alert("Database problem");

				}

				public void onSuccess(List<String> result) {
					Iterator<String> iterator = result.iterator();
					while (iterator.hasNext()) {
						schoolComboBoxReg.addItem(iterator.next());
					}
				}

			});

		}
		schoolComboBoxReg.addItem("m\u00E1s");
	}

	public MenuItem getMntmRolunk() {
		return mntmRolunk;
	}

	public void setMntmRolunk(MenuItem mntmRolunk) {
		this.mntmRolunk = mntmRolunk;
	}

	public MenuItem getMntmHirek() {
		return mntmHirek;
	}

	public void setMntmHirek(MenuItem mntmHirek) {
		this.mntmHirek = mntmHirek;
	}

	public MenuItem getMntmCikkek() {
		return mntmCikkek;
	}

	public void setMntmCikkek(MenuItem mntmCikkek) {
		this.mntmCikkek = mntmCikkek;
	}

	public MenuItem getMntmElerhetosegek() {
		return mntmElerhetosegek;
	}

	public void setMntmElerhetosegek(MenuItem mntmElerhetosegek) {
		this.mntmElerhetosegek = mntmElerhetosegek;
	}

	public MenuItem getMntmTudnivalok() {
		return mntmTudnivalok;
	}

	public void setMntmTudnivalok(MenuItem mntmTudnivalok) {
		this.mntmTudnivalok = mntmTudnivalok;
	}

	public MenuItem getMntmKezdolap() {
		return mntmKezdolap;
	}

	public void setMntmKezdolap(MenuItem mntmKezdolap) {
		this.mntmKezdolap = mntmKezdolap;
	}

	public void setUnSelectedMenuItem() {
		mntmKezdolap.setStyleName("MenuItem");
		mntmRolunk.setStyleName("MenuItem");
		mntmHirek.setStyleName("MenuItem");
		mntmCikkek.setStyleName("MenuItem");
		mntmElerhetosegek.setStyleName("MenuItem");
		mntmTudnivalok.setStyleName("MenuItem");

	}

	public MatlapGWT getThis() {
		return this;
	}
}
